package com.pro01.apirednote.controller;

import com.pro01.apirednote.dto.CommentRequest;
import com.pro01.apirednote.dto.CommentResponse;
import com.pro01.apirednote.pojo.PageResult;
import com.pro01.apirednote.pojo.Result;
import com.pro01.apirednote.service.CommentService;
import com.pro01.apirednote.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/contents")
@RequiredArgsConstructor
public class CommentController {

    private final CommentService commentService;

    // ✅ 发布评论接口
    @PostMapping("/{contentId}/comments")
    public Result<?> addComment(@PathVariable Long contentId,
                                @RequestBody CommentRequest request,
                                HttpServletRequest httpRequest) {
        Long userId = TokenUtil.getUserId(httpRequest);
        return commentService.addComment(userId, contentId, request);
    }

    // 用户查看评论列表
    @GetMapping("/{contentId}/comments")
    public Result<PageResult<CommentResponse>> getComments(
            @PathVariable Long contentId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return commentService.getCommentsByContent(contentId, pageNum, pageSize);
    }

    // 删除评论
    @DeleteMapping("/comments/{commentId}")
    public Result<Void> deleteComment(@PathVariable Long commentId, HttpServletRequest request) {
        Long userId = TokenUtil.getUserId(request);
        String role = TokenUtil.getRole(request);
        return commentService.deleteComment(commentId, userId, role);
    }
}